# -*- coding: UTF-8 -*-
'''
Created on 19.03.2012

@author: Alexei Kosarev
'''

import Tkinter as tk

class Table(object):
    '''Transliterator table:
        - transliterates symbol,
        - returns hint for current symbol table,
        - calls f on 'q'.
    '''
    def __init__(self, f):
        self.d = {
            'a' : u'Аа', 'b' : u'Бб', 'c' : u'Цц', 'd' : u'Дд',
            'e' : u'Ее', 'f' : u'Фф', 'g' : u'Гг', 'i' : u'Ии',
            'j' : u'Йй', 'k' : u'Кк', 'l' : u'Лл', 'm' : u'Мм',
            'n' : u'Нн', 'o' : u'Оо', 'p' : u'Пп', 'r' : u'Рр',
            's' : u'Сс', 't' : u'Тт', 'u' : u'Уу', 'v' : u'Вв',
            'w' : u'Шш', 'x' : u'Хх', 'z' : u'Зз',
            'h' : {'c' : u'Чч', 'm' : u'Ьь', 'n' : u'Ъъ', 's' : u'Щщ', 'z' : u'Жж'},
            'y' : {'a' : u'Яя', 'e' : u'Ээ', 'i' : u'Ыы', 'o' : u'Ёё', 'u' : u'Юю'},
            'q' : f,
            }

        def hint(d):
            '''Makes tree-like dict with hints'''
            qwerty_keyboard = u'q|w|e|r t y u|i|o|p\na|s|d|f g h j|k|l|;\nz|x|c|v b n m|,|.|/'
            h = {None : ''}
            for k in qwerty_keyboard:
                sym = k in self.d.keys() and '*' or k
                if k in d.keys():
                    if isinstance(d[k], unicode): sym = d[k][0]
                    elif isinstance(d[k], dict): h[k], sym = hint(d[k]), '+'
                h[None] += sym
            return h

        self.h = hint(self.d)
        self.d_, self.h_ = self.d, self.h

    def __call__(self, key):
        k = key.lower()
        if not k in self.d.keys(): return None

        try:
            v_ = self.d_[k]
        except KeyError:
            v_ = None

        sym = ''
        if hasattr(v_, '__call__'): v_()
        if isinstance(v_, unicode): sym = v_[k == key]
        self.d_, self.h_ = isinstance(v_, dict) and (v_, self.h_[k]) or (self.d, self.h)
        return sym

    def __str__(self):
        return self.h_[None]


class UI(tk.Frame):
    '''cut all
  to cliboard'''
    def __init__(self, master = None):
        tk.Frame.__init__(self, master)
        self.t = Table(self)
        self.text = tk.Text(self, font = ("Courier", "11"),
                            height = 9,
                            width = 32)
        self.label = tk.Label(self, font = ("Lucida Console", "11"),
                              height = 3,
                              width = 32,
                              anchor = 'nw',
                              text = self.t.__str__())
        self.text.grid(row = 1, column = 0)
        self.label.grid(row = 0, column = 0)
        self.grid()
        master.resizable(False, False)
        master.title('pyslct')
        self.text.bind('<KeyPress>', self.keypress)

    def __call__(self):
        self.text.clipboard_clear()
        self.text.clipboard_append(self.text.get('1.0', tk.END))
        self.text.delete('1.0', tk.END)
        

    def keypress(self, event):
        if (event.state | 11) == 11: # Shift, CapsLock, and NumLock
            sym = self.t(event.keysym)
            if sym is None: return
            self.text.insert(tk.INSERT, sym)
            if self.label['text'] != self.t.__str__():
                self.label['text'] = self.t.__str__()
            return('break')


UI(master = tk.Tk()).mainloop()
